package com.apesay.service.auth;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apesay.exception.ServiceException;
import com.apesay.service.utils.UtilsService;
import com.apesay.service.base.ClientService;
import com.apesay.utils.Constant;
import com.apesay.utils.DataUtils;
import org.apache.shiro.crypto.hash.Sha384Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AccountService {
	@Autowired
	private ClientService clientService;
	@Autowired
	private UtilsService utilsService;

	public JSONObject findByName(String userName)  {
		//删除标记，0正常，1逻辑删除 ；被逻辑删除的用户不在帐户列表中展示，不能登录系统
		JSONObject findObject= clientService.find("{username:'"+userName+"',delFlag:{$ne:1}}", "{}", "{}",
				Constant.model_backend_account,0,1);
		JSONArray result = findObject.getJSONArray(Constant.RESULTS);
		if(null!=result&&result.size()>0){
			return result.getJSONObject(0);
		} else {
			findObject= clientService.find("{email:'"+userName+"',delFlag:{$ne:1}}", "{}", "{}",
					Constant.model_backend_account,0,1);
			result = findObject.getJSONArray(Constant.RESULTS);
			if(null!=result&&result.size()>0){
				result = findObject.getJSONArray(Constant.RESULTS);
				return result.getJSONObject(0);
			}
		}
		return null;
	}
	
	public JSONObject updateAccount(String id,JSONObject jsonObject) {
		jsonObject.remove("password");
		return clientService.update(Constant.model_backend_account,id, jsonObject);
	}
	
	public JSONObject changePass(JSONObject jsonObject,Object userObj) {
		JSONObject reObject = new JSONObject();
		JSONObject user = JSON.parseObject(userObj.toString());
		String userId = user.getString(Constant.OBJECTID);
		//判断输入的原始密码是否正确
		String password = user.getString("password");
		String oldPass = jsonObject.getString("oldPass");
		String encryptedPassword=new Sha384Hash(oldPass).toBase64();
		if(!password.equals(encryptedPassword)){
			reObject.put("changepassword", "1");//原始密码输入错误
		}else{
			//修改密码
			String newPassword=new Sha384Hash(jsonObject.getString("newPass")).toBase64();
			jsonObject.put("password", newPassword);
			jsonObject.remove("oldPass");
			jsonObject.remove("newPass");
			jsonObject.remove("rePass");
			jsonObject.remove("isChangePass");
			reObject = clientService.update(Constant.model_backend_account,userId, jsonObject);
			reObject.put("changepassword", "2");//修改密码成功
		}
		return reObject;
	}
	
	public JSONObject rePass(String id){
		JSONObject reObject = new JSONObject();
		JSONObject jsonObject = new JSONObject();
		try {
			JSONObject where=new JSONObject();
			where.put("key", "初始化密码");
			where.put("group","init.password");//删除标记，0正常，1逻辑删除 ；被逻辑删除的用户不在帐户列表中展示，不能登录系统
			JSONObject one  = utilsService.getOne(where,Constant.model_dictionary); 
			if (DataUtils.isNotEmpty(one)) {
				//修改密码
				String newPassword = new Sha384Hash(one.getString("value")).toBase64();
				jsonObject.put("password", newPassword);
				jsonObject.remove("isRePass");
				reObject = clientService.update(Constant.model_backend_account,id,jsonObject);
				reObject.put("repassword", "1");//修改密码成功
			}
		} catch (Exception e) {
			reObject.put("repassword", "2");//修改密码失败
		}
		return reObject;
	}
	
	public JSONObject saveAccount(JSONObject jsonObject) {
		String plainPassword=jsonObject.getString("password");
		String encryptedPassword=new Sha384Hash(plainPassword).toBase64();
		jsonObject.put("password", encryptedPassword);
		jsonObject.put("delFlag", 0);//删除标记，0正常，1逻辑删除 ；被逻辑删除的用户不在帐户列表中展示，不能登录系统
		return clientService.add(Constant.model_backend_account,jsonObject);
	}
	/**
	 * 逻辑删除帐户
	 * @ 
	 * */
	public JSONObject delAccount(String accountId) {
		JSONObject accountObj = clientService.getObjectById(Constant.model_backend_account,accountId).getJSONObject(Constant.RESULT);
		accountObj.put("delFlag", 1);//删除标记，0正常，1逻辑删除 ；被逻辑删除的用户不在帐户列表中展示，不能登录系统
		return clientService.update(Constant.model_backend_account,accountId, accountObj);
	}
}
